
function  loadData(){
    $("#jqGrid").jqGrid($.extend(jqGrids.defaults,{
        url: base+'/role/view/list',
        colNames:[ 'ID','中文名称','英文名称','是否内置', '创建人', '创建时间','修改人','修改时间','状态','描述','<div align="center" >操作</div>'],
        colModel: [
            {name:'id',index:'id',hidden:true},
            {name:'name',index:'name',width:'80',  sortable:false},
            {name:'enname',index:'enname',width:'80',  sortable:false},
            {name:'isSys',index:'isSys',width:'60',  sortable:false,formatter:function(value, options, rData){return  value=='0'?'是':'否';}},
            {name:'createBy',index:'createBy', width:'80', sortable:false},
            {name:'createTime',index:'createTime', hidden:true, sortable:false,formatter:tools.formatDate},
            {name:'modifyBy',index:'modifyBy',hidden:true,  sortable:false},
            {name:'modifyTime',index:'modifyTime',  sortable:false,hidden:true,formatter:tools.formatDate},
            {name:'isEnabled',index:'isEnabled', width:'60', sortable:false,formatter:function(value, options, rData){return  value=='1'?'启用':'禁用';}},
            {name:'description',index:'description',width:'100', sortable:false},
            {name:'id',index:' ',align:'center',width:'150',sortable:false,formatter : function(value, options, rData){
                return tools.format("<a href='javascript:void(0)' onclick=doEdit(\'{0}\',\'{1}\')><span class='btn btn-sm purple'>编辑</span></a>",rData.id, rData.isSys)+
                    tools.format("<a href='javascript:void(0)' onclick=doDataRule(\'{0}\')><span class='btn btn-sm btn-primary'>数据范围</span></a>",rData.id);
            }}
        ]
    }));

}


function doEdit(id,isSys) {
    var url = base+ "/role/uc/toEdit?id=" + id ;
  if(isSys=="0"){
      $.messager.alert("内置角色不可编辑");
      return;
  }

    window.location.href=url;
}

function doAdd() {
    var url =base+ "/role/sk/toAdd";
    window.location.href=url;
}


/**
 *删除
 */
function doDelete() {
    var ids=$('#jqGrid').jqGrid('getGridParam','selarrrow');
    if (ids.length<=0) {
        $.messager.alert("请选择需要删除的数据项");
        return;
    }
    for(var id in ids){
        var rowData = $("#jqGrid").jqGrid('getRowData',ids[id]).isSys;
       if("是"==rowData){
           $.messager.alert("内置角色,不可删除");
           return;
       }
    }

    var url =base+"/role/dh/delete";
    $.messager.confirm("你确定要删除选中的记录吗?", function (ck) {
        if(ck){
            $.post(url, {ids: ids}, function (data) {
                $.messager.alert0({selector: "#indexAlert", type: data.code, html: data.message});
                if (data && data.code == 'SUCCESS') {
                   $("#jqGrid").trigger("reloadGrid");
                }
            });
        }})
}

/**查询*/
function doSearch(){
    var name=$("input[name=name]").val();
    var isEnabled=$("select[name=isEnabled]").val();
    $("#jqGrid").jqGrid('setGridParam',{
        datatype:'json',
        postData:{'name':name,'isEnabled':isEnabled}, //发送数据
        page:0
    }).trigger("reloadGrid"); //重新载入
}

function doSubmit(type) {
    if (!formCheck().form()) {
        return;
    }
    var url;

    if ("add" == type) {
        url = base + "/role/sk/insert";
    } else {
        var treeObj = $.fn.zTree.getZTreeObj("treeView");
        var nodes = treeObj.getCheckedNodes();
        var nodeIds = [];
        if (nodes && nodes.length > 0) {
            $.each(nodes, function (i, node) {
                nodeIds.push({id: node.id, checked: node.checked});
            });
        }
        $("input[name=checks]").val(JSON.stringify(nodeIds));

        url = base + "/role/uc/update";
    }
    $.post(url, $("#editForm").serialize(), function (data) {
        $.messager.alert0({selector: "#editAlert", type: data.code, html: data.message});
        if (data && data.code == 'SUCCESS') {
            tools.goto(base + "/role/view/index", 500);
        }
    }, "json");

}

function formCheck() {
    return $('#editForm').validate({
        rules: {
            name: {
                maxlength:[24],
                required:true
            },
            enname: {
                wRangeLength: [2, 24],
                required:true
            },
            appCode: {
                required:true
            }
        }
    });
}

/**
 * 数据规则授权界面
 */
function  doDataRule(id){
    var url = base+ "/role/view/dataRule/edit?id=" + id ;
    window.location.href=url;

}
/**
 * 角色分配资源
 */

function viewRes(){
    ////增加或者是系统内置角色的时候不显示资源列表
    if($("#isSys").val()=='0' || "edit"!=$("#operateType").val()){
        $(".edit-page-header").hide();
        return ;//系统内置角色不编辑授权
    }
    var id=$("#id").val();
    var code=$("#appCode").val();
    var setting = {
        async: {
            dataType: 'jsonp',
            type: "get",
            enable: true,
            url: base+"/resource/view/treeList?roleId="+id+"&code="+code
        },
        check: {
            enable: true,
            chkStyle: "checkbox",
            chkboxType: { "Y": "p", "N": "ps" }
        }
    };

    $.fn.zTree.init($("#treeView"), setting, null);


}

